//
//  CHCameraUIManage.m
//  SCCAProofSdk
//
//  Created by cyptt on 2022/3/10.
//

#import "CHCameraUIManage.h"
#import "CHRecordFile.h"
@interface CHCameraUIManage(){
    int persent;
    int seconds;
    int minutes;
}
@property(nonatomic,weak)UIViewController * currentController;
@property(nonatomic,assign)CH_RECORD_PHOTO_TYPE  recordType;

@property (nonatomic,strong )NSTimer *timer;

@property(nonatomic,strong)void (^successBlock)(id responseObj);
@property(nonatomic,strong)void (^failBlock)(NSError * error);
@property(nonatomic,assign)BOOL isSaveAblum; //是否保存相册

@property(nonatomic,strong)UIButton * flashBtn;
@end

@implementation CHCameraUIManage

-(instancetype)initWithController:(UIViewController *)controller recordPhotoType:(CH_RECORD_PHOTO_TYPE)recordPhotyType isSaveAblum:(BOOL)isSaveAblum SuccessBlock:(void (^ _Nonnull)(id responseObj))successBlock failBlock:(void (^ _Nonnull)(NSError * error))failBlock{
    if (self = [super init]) {
    
        self.recordPhotoType = recordPhotyType;
        self.currentController = controller;
        self.recordType = recordPhotyType;
        self.successBlock = successBlock;
        self.failBlock = failBlock;
        self.isSaveAblum = isSaveAblum;
        [self setUp];
    }
    return self;
}

-(void)setUp{
    
}


-(void)setChRecord:(CHBaseRecord * _Nonnull)chRecord{
    _chRecord = chRecord;
}
/*
 取消
 */
-(void)cancelBtnclick:(UIButton *)btn{
    [self pop];
    NSError * err = [NSError errorWithDomain:@"用户取消了" code:3333 userInfo:nil];
    if (self.failBlock) {
        self.failBlock(err);
    }
}

/*
 录制完成
 */
-(void)finishVideo:(NSURL *)filePath{
    [self pop];
    if (self.successBlock) {
        self.successBlock(filePath);
    }
}

/*
 失败
 */
-(void)errorCallback:(NSError *)error{
    if (self.failBlock) {
        self.failBlock(error);
    }
}
-(void)pop{
    if (self.currentController.navigationController) {
        [self.currentController.navigationController popViewControllerAnimated:YES];
    }else{
        [self.currentController dismissViewControllerAnimated:YES completion:nil];
    }
}
/*
 paly 点击
 */
-(void)palyBtnClick:(UIButton *)btn{
 
    if (self.recordType == CH_RECORD_PHOTO_TYPE_PHOTO) {
        
 
        [self.chRecord startRecordFilePath:nil isSaveAblum:self.isSaveAblum successBlock:^(NSURL * _Nonnull filePath) {
            
            [self finishVideo:filePath];
        } failBlock:^(NSError * _Nonnull error) {
            
            [self errorCallback:error];
     
        }];
    }else{
        
     
        if (btn.selected) {
            [self.chRecord startRecordFilePath:nil isSaveAblum:self.isSaveAblum successBlock:^(NSURL * _Nonnull filePath) {
                
                [self finishVideo:filePath];
            } failBlock:^(NSError * _Nonnull error) {
                [self errorCallback:error];
            }];
            
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(startTimer) userInfo:nil repeats:YES];
        
        }else{
          
            [self.chRecord stop];
            
            [self.timer invalidate];
            self.timer =nil;
        }
    }
   
}

-(void)startTimer{
    persent++;
    
    if(persent==60){
        seconds++;
        persent = 0;
    }
    if (seconds == 60) {
        minutes++;
        seconds = 0;
    }
    
    NSString  * resultTime = [NSString stringWithFormat:@"%02d:%02d.%02d",minutes,seconds,persent];
    
    if (resultTime) {
        self.timeStr = resultTime;
    }
    
}
/*
 闪光灯点击
 */
-(void)flashBtnClick:(UIButton *)btn{
   
 
    if (!btn.selected) {
        [self.chRecord openFlashSuccessBlock:^(BOOL isSuccess) {
            btn.selected = !btn.selected;
        } failBlock:^(NSString * _Nonnull errMsg) {
            
        }];
    }else{
        btn.selected = !btn.selected;
        [self.chRecord  closeFlash];
        
    }

}

/*
 翻转摄像头点击
 */
-(void)transferBtnClick:(UIButton *)btn{
    NSLog(@"%s",__func__);

    [self.chRecord transferCapture];
}
@end
